/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tagcloud;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import tagcloud.component.Article;
import tagcloud.component.Word;

/**
 *
 * @author thong
 */
public class Main {

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(2015, 05, 10);
        Date from = c.getTime();
        c.set(2015, 05, 11);
        Date to = c.getTime();

        //get number of day from T1 to T2;
        int numberOfDays = Math.round((to.getTime() - from.getTime()) / (86400000)) + 1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(from);

        //get all stemmed articles
        Article[] articleLemonde = new Article[numberOfDays];
        for (int i = 0; i < numberOfDays; i++) {
            calendar.add(Calendar.DATE, 1);  // number of days to add
            articleLemonde[i] = getArticleOn(calendar.getTime(), "lemonde");
        }

        TagCloud tagCloudLemonde = new TagCloud(articleLemonde, 20);
        Histogram histoLemonde = new Histogram(articleLemonde, 20);

        //get all stemmed articles
        Article[] articleFigaro = new Article[numberOfDays];
        for (int i = 0; i < numberOfDays; i++) {
            calendar.add(Calendar.DATE, 2);  // number of days to add
            articleFigaro[i] = getArticleOn(calendar.getTime(), "figaro");
        }

        TagCloud tagCloudFigaro = new TagCloud(articleFigaro, 20);
        Histogram histoFigaro = new Histogram(articleFigaro, 20);

        //get all stemmed articles
        Article[] articleLiber = new Article[numberOfDays];
        for (int i = 0; i < numberOfDays; i++) {
            calendar.add(Calendar.DATE, 3);  // number of days to add
            articleLiber[i] = getArticleOn(calendar.getTime(), "liberation");
        }

        TagCloud tagCloudLiber = new TagCloud(articleLiber, 20);
        Histogram histoLiber = new Histogram(articleLiber, 20);
        LatentSemantic latent = new LatentSemantic(histoLemonde.getWordWithValue(), histoFigaro.getWordWithValue(), histoLiber.getWordWithValue(), "lemonde", "lefigaro", "liberation");
        drawImage(tagCloudLiber.getImage(), "c_liberation.png");
        drawImage(tagCloudFigaro.getImage(), "c_figaro.png");
        drawImage(tagCloudLemonde.getImage(), "c_lemonde.png");
        drawImage(histoLiber.getImage(), "h_liberation.png");
        drawImage(histoLemonde.getImage(), "h_lemonde.png");
        drawImage(histoFigaro.getImage(), "h_figaro.png");
        drawImage(latent.getImage(), "cluster.png");
    }

    public static void drawImage(BufferedImage image, String fileName) {
        try {
            ImageIO.write(image, "png", new File(fileName));
        } catch (IOException ex) {
            Logger.getLogger(TagCloud.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static Article getArticleOn(Date T, String journal) {
        Article article = new Article();
        //test
        Calendar c = Calendar.getInstance();
        c.setTime(T);
        if (c.get(Calendar.DATE) % 3 == 0) {
            article.words.add(new Word("Love", 110));
            article.words.add(new Word("White", 30));
            article.words.add(new Word("NewYork", 5));
            article.words.add(new Word("Kill", 10));
            article.words.add(new Word("Sweet", 43));
            article.words.add(new Word("Kiss", 50));
            article.words.add(new Word("Couple", 34));
            article.words.add(new Word("Break", 8));
            article.words.add(new Word("Gun", 35));
            article.words.add(new Word("Bin", 50));
            article.words.add(new Word("Shot", 34));
            article.words.add(new Word("Bomb", 8));
            article.words.add(new Word("Paris", 35));
            article.words.add(new Word("Summer", 8));
            article.words.add(new Word("Hot", 35));
            article.words.add(new Word("Sun", 50));
            article.words.add(new Word("IS", 34));
            article.words.add(new Word("Siri", 8));
            article.words.add(new Word("Obamar", 35));
        } else if (c.get(Calendar.DATE) % 3 == 1) {
            article.words.add(new Word("Love", 100));
            article.words.add(new Word("Chocolate", 30));
            article.words.add(new Word("Rose", 5));
            article.words.add(new Word("Kill", 10));
            article.words.add(new Word("Sweet", 43));
            article.words.add(new Word("Bomb", 50));
            article.words.add(new Word("Woman", 34));
            article.words.add(new Word("Break", 6));
            article.words.add(new Word("Bin", 35));
            article.words.add(new Word("Obamar", 55));
            article.words.add(new Word("France", 11));
            article.words.add(new Word("Paris", 4));
            article.words.add(new Word("Graduate", 8));
            article.words.add(new Word("School", 35));
            article.words.add(new Word("Sea", 50));
            article.words.add(new Word("Holiday", 34));
            article.words.add(new Word("Attack", 8));
            article.words.add(new Word("Plane", 35));
        } else {
            article.words.add(new Word("Il", 100));
            article.words.add(new Word("etait", 30));
            article.words.add(new Word("une", 5));
            article.words.add(new Word("fois", 10));
            article.words.add(new Word("un", 43));
            article.words.add(new Word("petit", 50));
            article.words.add(new Word("garcon", 34));
            article.words.add(new Word("qui", 6));
            article.words.add(new Word("vivait", 35));
            article.words.add(new Word("dans", 55));
            article.words.add(new Word("une", 11));
            article.words.add(new Word("grande", 4));
            article.words.add(new Word("maison", 8));
            article.words.add(new Word("sa", 35));
            article.words.add(new Word("vie", 50));
            article.words.add(new Word("n'estait", 34));
            article.words.add(new Word("que", 8));
            article.words.add(new Word("joie", 35));
        }
        return article;
    }
}
